package com.tanzc.mp3player;

import com.tanzc.mp3player.filter.FilenameSuffixFilter;
import com.tanzc.mp3player.utils.ConfigReader;
import com.tanzc.mp3player.utils.ProcessUtil;
import sun.jvmstat.monitor.MonitorException;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * <p> description:
 * 管理Mp3Player，包括关闭，重启等
 * 此类准确来说只是管理导出的jar包，没有与Mp3Player直接联系
 * </p>
 *
 * @author tanzc
 * @date 14:02 2021/5/21
 **/
public class Mp3PlayerManager {

    String music_player;

    ProcessUtil processUtil;

    public static void main(String[] args) {
        String op, mp3Dir;
        if (args.length == 0) {
            op = "kill";
            mp3Dir = null;
        } else {
            op = args[0];
            mp3Dir = args.length > 1 ? args[1] : "G:\\music\\mp3";
        }
        File mp3Temp = new File(mp3Dir);
        if(!mp3Temp.isDirectory() || mp3Temp.list(new FilenameSuffixFilter("mp3")).length == 0) {
            System.out.println("invalid mp3 directory path!");
            return;
        }

        Mp3PlayerManager manager = new Mp3PlayerManager();
        try {
            switch (op) {
                case "kill":
                    if(!manager.killMp3Player()) {
                        System.out.println("no music-player.jar running!");
                    }
                    break;
                case "start":
                    manager.startMp3Player(mp3Dir);
                    break;
                case "restart":
                    manager.restartMp3Player(mp3Dir);
                    break;
                default:
                    System.out.println("invalid operation!");
                    break;
            }

        } catch (IOException | URISyntaxException | MonitorException e) {
            e.printStackTrace();
        }
    }

    public Mp3PlayerManager() {
        Map<String, Object> ini = ConfigReader.readIni("./src/config.ini");
        music_player = ((Map<String, String>)ini.get("path")).get("music_player");
        processUtil = new ProcessUtil();
    }

    public boolean startMp3Player(String mp3Dir) throws IOException {
        processUtil.startWithRuntimeExec("java -jar " + music_player + " " + mp3Dir);
        return true;
    }

    public boolean killMp3Player() throws URISyntaxException, MonitorException, IOException {
        boolean flag = false;
        Map<Object, String> vmMap = processUtil.getAllRuningJava();
        for (Map.Entry<Object, String> entry : vmMap.entrySet()) {
            System.out.println(entry.getKey() + " ---> " + entry.getValue());
            if (entry.getValue().contains("music-player.jar")) {
                processUtil.killProcess(Integer.parseInt(entry.getKey().toString()));
                flag = true;
            }
        }
        return flag;
    }

    public boolean restartMp3Player(String mp3Dir) throws URISyntaxException, IOException, MonitorException {
        killMp3Player();
//        new Thread(()->{
//            try {
//                // 使用ProcessBuilder直接java -jar会造成阻塞，创建的新进程mp3player在mp3playermanager管理下。
////                processUtil.startProcess("java","-jar","F:\\projects\\idea\\music-player\\out\\artifacts\\music_player_jar\\music-player.jar","G:\\music\\mp3");
//                // 使用ProcessBuilder执行bat，通过bat去执行mp3player，这样就不会阻塞了，新进程属于cmd管理下，但是bat又不能穿参数进去，改不了音乐目录
////                processUtil.startProcess("G:\\quickStart\\usual\\mymp3.bat");
//                // 使用Runtime.getRuntime().exec() java -jar 可以执行jar并且不造成阻塞，同时可以传进参数，这个比较合适。
////                processUtil.startProcess2("G:\\music\\mp3");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }).start();

//        processUtil.startWithProcessBuilder("java", "-jar", "F:\\projects\\idea\\music-player\\out\\artifacts\\music_player_jar\\music-player.jar", "G:\\music\\mp3");
//        processUtil.startWithProcessBuilder("G:\\quickStart\\usual\\mymp3.bat");
        startMp3Player(mp3Dir);

        return true;
    }
}


